package upgrade

import (
	"duoke/utils/gconv"
	"duoke/utils/gmd5"
	"fmt"
	"github.com/tidwall/gjson"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

var IsAuthorize bool = false

// 判断是否授权
func CheckAuthorize() bool {

	resp, err := http.Get(GetAuthUrl())
	if err != nil {
		fmt.Println("get failed, err:", err)
		return false
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("read from resp.Body failed,err:", err)
		return false
	}
	result := string(body)
	// fmt.Println(result)
	sign := gjson.Get(result, "sign")
	if !sign.Exists() {
		fmt.Println("授权失败1")
		IsAuthorize = false
		return false
	}
	if gmd5.MustEncryptString(gconv.String(3)+"wx:ywdoing"+time.Now().Format("2006-01-02")) != sign.Value() {
		fmt.Println("授权失败2")
		IsAuthorize = false
		return false
	}
	// fmt.Println("获取内容", result)
	if find := strings.Contains(result, "success"); find {
		// fmt.Println("授权成功")
		IsAuthorize = true
		return true
	}
	if find := strings.Contains(result, "SUCCESS"); find {
		// fmt.Println("授权成功")
		IsAuthorize = true
		return true
	}
	fmt.Println("授权失败3")
	IsAuthorize = false
	return false
}
